package com.bdqn.t330.ch12.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/file")
public class FileController {


    @RequestMapping("/index")
    public String index() {
        System.out.println("跳转到上传页面");
        return "/file/index";
    }

    @RequestMapping("/upload")
    public String upload(HttpServletRequest request, String uname, MultipartFile idPic) {
        if (idPic != null) {
            String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
            System.out.println("path:" + path);
            File targetFile = new File(path);
            if (!targetFile.exists()) {
                targetFile.mkdirs();
            }
            // String path  = request.getSession().getServletContext().getRealPath("static" + "/" + "uploadfiles");
            //String path = "D:\\img";
            try {
                //idPic.transferTo(new File("D:\\img","test.png"));
                idPic.transferTo(new File(targetFile, "test.png"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "/file/index";
    }
}
